//prepare long-form census sample for matching to PIKs



//loop over states
local states `"ak al ar az ca co ct de fl ga hi ia id il in ks ky la ma md me mi mn mo ms mt nc nd ne nh nj nm nv ny oh ok or pa ri sc sd tn tx ut va vt wa wi wv wy"'
local i = 0
qui{
foreach st in `states'{
	local i `++i'
	noi di as result "Working on state `i': `st'"

	//step 1: read in geography file. keep only a few things
	use "$data/Census/2000/stata/census_sedf_`st'_01", clear
	keep bcseq state county tract block blkgrp ur //basic geography
	tempfile block
	save `block'

	//step 2: read in housing file. keep only a few things
	use "$data/Census/2000/stata/census_sedf_`st'_02", clear
	keep bcseq hseq mafid srent hinc finc rrt smortgd smhcost sconfee stenure sbldgsz svalue smortg hwt
	tempfile house
	save `house'

	//person-level data
	use "$data/Census/2000/stata/census_sedf_`st'_03", clear
	keep bcseq hseq qage qincwg qwklyrwk qwklyrhr qinctot pwt pern pov qsex qrel pnc ///
	pseq qdb imprace qms qhigh qgrade qattend qspeak qcitizen qpobst qmigpl qmigst qmigco esr qind qocc qcow ///
	racew raceb disable msp qspan

	//create birth year variable
	gen birth_yr = substr(qdb, 1, 4)
	destring birth_yr, replace

	merge m:1 bcseq using `block', keep(match) nogen //merge on geography
	merge m:1 hseq using `house', keep(match) nogen //merge on housing stuff
	gen temp = (birth_yr>=1983 & birth_yr<=1994) //relaxed range to be safe

	//only keep if appropriately-aged kid is in the household
	bys mafid: egen kid = max(temp)
	keep if kid
	drop temp kid //nix unecessary variables
	compress
	save "$temp/census_long_`st'", replace //save
}
}

exit //end of dofile

